package com.bocloud.cmp.driver.providers.vmware;

import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.bocloud.cmp.driver.providers.Driver;
import com.vmware.vim25.mo.Folder;
import com.vmware.vim25.mo.ServiceInstance;

/**
 * vmware连接验证通用类
 * 
 * @author weiwei
 *
 */
public class VmwareDriver implements Driver {
	public static final String VM = "VirtualMachine";
	public static final String DT = "Datastore";
	public static final String CR = "ComputeResource";
	public static final String DC = "Datacenter";

	private static Logger logger = LoggerFactory.getLogger(VmwareDriver.class);
	private String endpoint;
	private String username;
	private String password;
	private URL url;
	private ServiceInstance serviceInstance;
	private Folder rootFolder;
	private boolean accessable;

	/**
	 * @return the rootFolder
	 */
	public Folder getRootFolder() {
		return rootFolder;
	}

	/**
	 * @return the accessable
	 */
	public boolean isAccessable() {
		return accessable;
	}

	public String getEndpoint() {
		return endpoint;
	}

	public void setEndpoint(String endpoint) {
		this.endpoint = endpoint;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @return the serviceInstance
	 */
	public ServiceInstance getServiceInstance() {
		return serviceInstance;
	}

	/**
	 * @return the url
	 */
	public URL getUrl() {
		return url;
	}

	public VmwareDriver() {
		super();
	}

	public void init() {
		try {
			url = new URL("https", endpoint, "/sdk");
			serviceInstance = new ServiceInstance(url, username, password, true);
			rootFolder = serviceInstance.getRootFolder();
			this.accessable = true;
		} catch (MalformedURLException | RemoteException e) {
			this.accessable = false;
			logger.error("Init Driver Error:", e);
		}
	}

	public VmwareDriver(String endpoint, String username, String password) {
		super();
		this.endpoint = endpoint;
		this.username = username;
		this.password = password;
		this.init();
	}

	@Override
	public boolean accessable() {
		return this.accessable;
	}

}
